package client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import javax.swing.*;
import sharedspace.MyFile;
import sharedspace.*;

import messages.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
/**
 * This JPanel purpose is to show the entire exams in the database
 * @author G3
 *  *
 */
import client.GetFileFromServer;

/**
 * This JPanel is a panel for Manager, to see all exams that was made.
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */


public class GUI_Manager_ViewExams extends GUI_AbstractMainMenu{
	private JLabel lblChooseExam = null;
	private JScrollPane examsScroller = null;
	private JTable examJTable = null;
	private List<Test> exList = null;
	private ArrayList<QuestionInTest> qitList = null;
	private JButton testBtn = null;
	public JButton btnBack = null;

	Client_GUI ms=null;

	public GUI_Manager_ViewExams(Client_GUI ms) {
		super(MainMenuType.MANAGER,ms);
		this.ms = ms;
		this.initiateManager();
		greetMsg.setText("View Exams");
		mainMenu.setText("Manager");

	}
	
	/**
	 * Building the panel contents 
	 */
	public void initiateManager()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbtnBack(),null);
		background.add(getExamsTable());
		background.add(getTestButton());
		lblChooseExam = new JLabel("Choose an Exam:");
		lblChooseExam.setForeground(Color.BLUE);
		lblChooseExam.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
		lblChooseExam.setBounds(349, 120, 147, 34);
		background.add(lblChooseExam);


	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}
	
	private JButton getTestButton()
	{
		if (testBtn == null)
		{
			testBtn = new JButton("Show Test");	
			testBtn.setBounds(450, 600, 100, 30);
			testBtn.setVisible(true);
			testBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					System.out.println(examJTable.getSelectedRow());
					String testID = exList.get(examJTable.getSelectedRow()).getTestID();
					 String str="manual test";
					 if (str.equalsIgnoreCase(exList.get((examJTable.getSelectedRow())).getCommentForStudent())==true){
						 showManualTest();
						 JOptionPane.showMessageDialog(null,"Download was complete");
						 }
						
					 else{
					MessageQuestionListByTestID MsgGql = new MessageQuestionListByTestID(testID);
					try {
						ms.client.sendToServer(MsgGql);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					qitList = (ArrayList<QuestionInTest>)ms.client.GetMessage();
					ArrayList<QuestionInTest> alist = new ArrayList<QuestionInTest>();
					alist = qitList;
					GUI_Test_View testFrame = new GUI_Test_View(qitList);
					testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					testFrame.setResizable(false);
					testFrame.setVisible( true ); // display frame
					 }//end else
				}
			});
		}
		
		return testBtn;
	}
	
		public void showManualTest()
		{
			String str;
			str=findTestPath(exList.get((examJTable.getSelectedRow())).getTestID());
			ExecuteTest exT =new ExecuteTest();
			exT.setWordTestPath(str);
			try
			{
				MessageGetWordFile msggwf = new MessageGetWordFile(exT);
				ms.client.sendToServer(msggwf);
				MyFile myFile = (MyFile)ms.client.GetMessage();
				File file = FileHandler.saveFileDialog("Save Test");
			   	String path = file.getAbsolutePath();
			    GetFileFromServer gffs = new GetFileFromServer(path);
				gffs.getFileFromServer(myFile);
				
			    			
			} 
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}// end showManualTest
		
		public String findTestPath(String testID) 
		{
			
			MessageGetManualByTestID MbtI = new MessageGetManualByTestID (testID);
			try{
				ms.client.sendToServer(MbtI);// get list of teacher subject
				
				ManualTest manualTest = (ManualTest)ms.client.GetMessage();
				
				 return manualTest.getPath();
				
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			return null;
		}
			

	
	@SuppressWarnings("unchecked")
	public JScrollPane getExamsTable()
	{
		if (examJTable == null)
		{
			MessageAllExams msgExams=new MessageAllExams();
			try {
				ms.client.sendToServer(msgExams);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			exList = (ArrayList<Test>)ms.client.GetMessage();
			String columnNames[] = {"ID","Subject","Author","Duration","Comment for Student","Comment for Teacher"};
			String tableModel[][] = new String[exList.size()][Teacher.NUM_OF_COLUMNS];
			for (int i=0; i<exList.size(); i++){
				tableModel[i][0] = exList.get(i).getTestID();
				tableModel[i][1] = exList.get(i).getSubject().getSubjectName();
				tableModel[i][2] = (exList.get(i).getAuthor().getUserFirstName() + " " + exList.get(i).getAuthor().getUserLastName());
				tableModel[i][3] = "" + exList.get(i).getTestDuration();
				tableModel[i][4] = exList.get(i).getCommentForStudent();
				tableModel[i][5] = exList.get(i).getCommentForTeacher();
			}
				examJTable = new JTable(tableModel, columnNames){
					   public boolean isCellEditable(int rowIndex, int colIndex) {
					   return false; //Disallow the editing of any cell
					   }
				   };		 
			examJTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
			examJTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			examJTable.getTableHeader().setReorderingAllowed(false);
			examJTable.setRowSelectionInterval(0, 0);
			examsScroller= new JScrollPane(examJTable);
			examsScroller.setBounds(50, 150, 900, 400);
		}
			
		
		return examsScroller;
	}
}
